package zzr.library;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;

import java.time.LocalDateTime;
import java.util.List;

import zzr.library.adapter.BorrowItemAdapter;
import zzr.library.databinding.ActivityReaderBinding;
import zzr.library.databinding.DialogBorrowBinding;
import zzr.library.db.common.AppDatabase;
import zzr.library.db.common.DBUtil;
import zzr.library.db.obj.BookDO;
import zzr.library.db.obj.BorrowDO;
import zzr.library.db.obj.ReaderDO;
import zzr.library.db.obj.combine.BorrowWithBook;

public class ReaderActivity extends AppCompatActivity {
    private ActivityReaderBinding binding;
    private AppDatabase appDatabase;
    private BorrowItemAdapter adapter;

    private ReaderDO loginReader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityReaderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        appDatabase = DBUtil.getAppDatabase(this);
        Intent intent = getIntent();
        int rid = intent.getIntExtra("rid", -1);
        loginReader = appDatabase.readerDAO().selectById(rid);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("欢迎你，" + loginReader.name);

        List<BorrowWithBook> borrowWithBooks = appDatabase.borrowDAO().selectByReaderId(rid);

        RecyclerView borrowList = binding.borrowList;
        borrowList.setLayoutManager(new LinearLayoutManager(this));
        borrowList.setAdapter(new BorrowItemAdapter(borrowWithBooks));

        binding.borrowBtn.setOnClickListener(v -> {
            DialogBorrowBinding dialogBorrowBinding = DialogBorrowBinding.inflate(getLayoutInflater());
            new AlertDialog.Builder(this)
                    .setView(dialogBorrowBinding.getRoot())
                    .setTitle("借书")
                    .setPositiveButton("借阅此书", (dialog, which) -> {
                        int bid = 0;
                        try {
                            bid = Integer.parseInt(dialogBorrowBinding.borrowBid.getText().toString());
                        } catch (NumberFormatException e) {
                            Snackbar.make(binding.getRoot(), "键入正确的书号", Snackbar.LENGTH_LONG).show();
                            return;
                        }
                        BookDO bookDO = appDatabase.bookDAO().selectById(bid);
                        bookDO.in = false;
                        appDatabase.bookDAO().update(bookDO);
                        BorrowDO borrowDO = new BorrowDO(0, bid, loginReader.rid, LocalDateTime.now(), LocalDateTime.now().plusDays(60), null, false);
                        appDatabase.borrowDAO().insert(borrowDO);
                        Snackbar.make(binding.getRoot(), "成功借阅书籍：" + bookDO.name, Snackbar.LENGTH_LONG).show();
                    }).create().show();
        });

    }

}
